﻿using System;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Data.Infrastructure;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.Contact;

namespace Guan.CMS.Domain.Handlers.Contact
{
    public class AddContactHandler : ICommandHandler<AddContactCommand>
    {
        private readonly IContactRepository _contactRepository;
        private readonly IUnitOfWork _unitOfWork;

        public AddContactHandler(IContactRepository contactRepository, IUnitOfWork unitOfWork)
        {
            this._contactRepository = contactRepository;
            this._unitOfWork = unitOfWork;
        }

        public ICommandResult Execute(AddContactCommand command)
        {
            var model = new Model.Entities.Contact()
                            {
                                Name = command.Name,
                                CreatedTime = DateTime.Now,
                                Email = command.Email,
                                IP = command.IP,
                                Message = command.Message,
                                Title = command.Title
                            };
            _contactRepository.Add(model);
            _unitOfWork.Commit();
            return new CommandResult(true);
        }
    }
}